package dhee.wtqshopproject.dao;

import dhee.wtqshopproject.entity.News;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NewsMapper {

    // 新增新闻（返回自增主键）
    @Insert("INSERT INTO hxshop.news (title, content, admin_id, publish_time) " +
            "VALUES (#{title}, #{content}, #{adminId}, now())")
    @Options(useGeneratedKeys = true, keyProperty = "newsId")
    void insertNews(News news);

    // 删除新闻
    @Delete("DELETE FROM hxshop.news WHERE news_id = #{newsId}")
    void deleteNews(Integer newsId);

    // 修改新闻
    @Update("UPDATE hxshop.news SET " +
            "title = #{title}, " +
            "content = #{content}, " +
            "admin_id = #{adminId}, " +
            "publish_time = now() " +
            "WHERE news_id = #{newsId}")
    void updateNews(News news);

    // 查询所有新闻（按发布时间倒序）
    @Select("SELECT * FROM hxshop.news ORDER BY publish_time DESC")
    List<News> selectAllNews();

    // 根据ID查询单个新闻
    @Select("SELECT * FROM hxshop.news WHERE news_id = #{newsId}")
    News selectNewsById(Integer newsId);




}
